var myTeam = [0,0,0,0,0,0,0,0,0,0,0];

var totalValue = 1000;

var currentTeamValue = 0;

var totalPlayers = 0;

var status= [];

var count = 0;





var ShowPlayers = function(id,op){
var url= getBaseURL();

	if(op == 'add')

	{

		loadPage(url+'team/teamplayers/index/id/'+ id, 'player-list' );

	}

	else

	{

		loadPage(url+'team/teamplayers/edit/id/'+ id, 'player-list' );

	}

};

var ShowPlayerDetails = function(id){
var url= getBaseURL();

	loadPage(url+'team/teamplayers/thumb/id/'+ id, 'player-details' );

};



var addPlayer = function( content){

	





	details = content.split("-");

	

	id = details[0];

	var role = details[3].replace(/^\s+|\s+$/g,"");

	for(i=0;i<11;i++){

		

		//checking condition for only 3 players from a single team

			var counter=1;

			$("input[name='teamID']").each(function(){

			if($(this).val() == details[4]){

				counter++;

			}

			});

			if(counter > 3)

			{

				alert("Only 3 players should be selected from one team");

				break;

			}

			

			//checking condition for player type 

			var batsCounter=1;

			var bowlersCounter=1;

			var allRounderCounter=0;

			var WicketCounter=0;

			$("input[name='status']").each(function(){

			if($(this).val() == "B" || $(this).val() == "A"){

				batsCounter++;

			}

			if($(this).val() == "O"){

				bowlersCounter++;

			}

			/*if($(this).val() == "A"){

				allRounderCounter++;

			}*/

			if($(this).val() == "W"){

				WicketCounter++;

			}

			});

			if(role == "B")

			{

				if(batsCounter > 5)

				{

					alert("Team should have only 5 batsman");

					break;

					

				}

			}

			if(role == "O")

			{

				//if(allRounderCounter == 0)

				//{

					if(bowlersCounter > 5)

					{

						alert("Team should have only 5 bowlers");

						break;

					}

				//}

				/*else

				{

					if(bowlersCounter > 4)

					{

						alert("Team should have only 4 bowlers");

						break;

					}

				}*/

			}

			/*if(role == "A")

			{

				if(bowlersCounter > 5)

				{

					

					if(allRounderCounter ==0)

					{

						alert("Team should not have Allrounder");

						break;

						

					}

				}

				else

				{

					if(allRounderCounter >=1)

					{

						alert("Team should  have only 1 Allrounder");

						break;

					}

				}

			}

			*/

			if(role == "W")

			{

				if(WicketCounter ==1)

				{

					alert("Team should have only 1 Wicket Keeper");

					break;

					

				}

			}

			

			//checking condition for unique player

			if(myTeam[i]!="" && myTeam[i] != 0){

				if(myTeam[i] == id){

					alert("Player already present in your team!!!");

					break;

				}

				

			}			

			else{

			

				myTeam[i] = id;

				points = parseInt(details[2]);

				//currentTeamValue += 

				var type;

				if(role == 'B'|| role == 'A')

				{

					type = 'Batsman';

					

					//Batsman count

						remBatsCounter     = 5 - batsCounter; 

						$("#available_batsman").html((remBatsCounter));

						$("#available_batsmanSec").html((remBatsCounter));

				}

				if(role == 'O')

				{

					type = 'Bowler';

					

					//Bowler count

						if(bowlersCounter <=4)

						{

							remBowlCounter   = 4 - bowlersCounter;

						}

						else

						{

							remBowlCounter   = 0;

						}

						remBowlCounterSec   = 5 - bowlersCounter;

						$("#available_bowler").html((remBowlCounter));

						$("#available_bowlerSec").html((remBowlCounterSec));

				}

				if(role == 'W')

				{

					type = 'Wicket Keeper';

					

					//Wicket count

						remWickCounter     = 0;

						$("#available_keeper").html((remWickCounter));

						$("#available_keeperSec").html((remWickCounter));

					

				}

				if(role == 'A')

				{

					type = 'All Rounder';

					

					//Allrounder count

						remRounCounter     = 0;

						//$("#available_allrounder").html((remRounCounter));

						//$("#available_allrounderSec").html((remRounCounter));

				}

				

				var imgPath = document.getElementById("imgPath"+id).value;

				if(imgPath!="")

				{
                                     var url= getBaseURL();

					var img = '<img src="/files/players/'+imgPath+'" style="float:left; width:69px; height:90px; margin-right:5px;" />';

				}

				else

				{

					var img ="";

				}

				

				htmlVal = '<div><ul><li class="selected_players"><div> <div style="float: left;"><input type="hidden" name="team[]" value="'+ id +'" /><input type="hidden"  name="status" value="'+role+'"/><input type="hidden"  name="teamID" value="'+details[4]+'"/>'+img+' <br /> <a class="player_stats_remove" href="javascript: void(0);" onclick="removePlayer('+ id +','+ points +','+'\''+role+'\''+');">Remove</a></div> <p style="font-size: 1.1em; font-weight: bold; display: block;"> '+ details[1] +'</p> <br> <p>'+ type + '</p> <p>' + details[2] + ' points </p> <p> <div id="captain-selection" style="display:none;"> <input type="radio" name="is_captain[]" id="is_captain" value="' + id +'" onclick="teamCaptain();"></div></p>   </div></li></ul></div>';

				

				

				

				$("#player-"+ (i+1)).html(htmlVal);

				//$("#used-points").html(currentTeamValue);

				//$("#balance-points").html((totalValue-currentTeamValue));

				

				//Displaying the final selection pattern of players

				

				    var val =1;

				  	var batVal    = batsCounter - val;

					var bowVal    = bowlersCounter;

					//var roundVal  = allRounderCounter;

					var wicketVal = WicketCounter;

					

				htmlCont ='<p>Batsman        :'+batVal+'</p>';

				htmlCont +='<p>Bowlers       :'+bowVal+'</p>';

				//htmlCont +='<p>All Rounder   :'+roundVal+'</p>';

				htmlCont +='<p>Wicket Keeper :'+wicketVal+'</p>';
				
				var checkpoints=parseFloat(document.getElementById('used-points').innerHTML) + (points);
			//	alert(checkpoints);
				
				if(document.getElementById('used-points').innerHTML <=1000 && checkpoints<=1000)
				{
					
				
				pointUpdate(points, "add");
				}
				

					

				

				totalPlayers++;	

				

				if(totalPlayers == 11)

				{

					

					if(document.getElementById('used-points').innerHTML >1000)

					{

						alert("Points of player should not exceed more than 1000");

					}

					else

					{

						$("#select-card").hide();

						$("#select-captain-step").show();

						$("div#captain-selection").show();						

						$("#selection").html(htmlCont);

					}

					/*else

					{	

						// enable button

						$("#select-card").hide();

						$("#create-team-step").show();

						$("#selection").html(htmlCont);

					}*/

				}

				

				break;

				

			}

		}

		

};







var removePlayer = function(id, points,role){

	

	if(totalPlayers == 11){

		// enable button

		$("#select-card").show();

		$("#create-team-step").hide();

		$("#select-captain-step").hide();

		$("div#captain-selection").hide();	

		

	}

	for(i=0;i<11;i++){

		if(myTeam[i] == id){

			myTeam[i] = "";

			document.getElementById('player-'+ (i+1)).innerHTML = "";

			totalPlayers--;

			

			pointUpdate(points, "sub");

		}

		

	}

	

	//Displaying the count of players remaining

	if(role == 'B' || role == 'A')

	{

	

		//Batsman count

		$batsCnt = $("#available_batsmanSec").html();

		remBatsCounter   = parseInt($batsCnt)+1; 

		$("#available_batsman").html((remBatsCounter));

		$("#available_batsmanSec").html((remBatsCounter));

	}

	if(role == 'O')

	{

		//Bowler count

			$bowlCnt = $("#available_bowlerSec").html();

			remBowlCounter     =parseInt($bowlCnt)+1 ;

			$("#available_bowler").html((remBowlCounter));

			$("#available_bowlerSec").html((remBowlCounter));

	}

	

	/*if(role == 'A')

	{

		//Allrounder count

		$rouderCnt  = $("#available_allrounder").html();

		remRounCounter     = parseInt($rouderCnt)+1;

		$("#available_allrounder").html((remRounCounter));

		

	}*/

	if(role == 'W')

	{

		$wicCnt = $("#available_keeperSec").html();

		//Wicket count

			remWickCounter     = parseInt($wicCnt)  + 1;

			$("#available_keeper").html((remWickCounter));

			$("#available_keeperSec").html((remWickCounter));

		

	}

};





//function to remove player while editing

var changePlayer = function(id, points,role,i,teamId){

	

	//setting the count of players removed	

		playersCnt = playersCnt -1;



	//getting the id of the id of li from where player is removed

		PlayerId   = i;

	

		document.getElementById('player-'+i).innerHTML = "";

		

	//Calculating remaining points when a player is removed from the list	

		editPointUpdate(points, "sub");

		

	//Displaying the count of players remaining

		if(role == 'B' || role == 'A')

		{

	

			//Batsman count

			$batsCnt = $("#available_batsmanSec").html();

			remBatsCounter   = parseInt($batsCnt)-1; 

			$("#available_batsman").html((remBatsCounter));

			$("#available_batsmanSec").html((remBatsCounter));

		}

		if(role == 'O')

		{

			//Bowler count

				$bowlCnt = $("#available_bowlerSec").html();

				remBowlCounter     =parseInt($bowlCnt)-1 ;

				$("#available_bowler").html((remBowlCounter));

				$("#available_bowlerSec").html((remBowlCounter));

		}	

		if(role == 'W')

		{

			$wicCnt = $("#available_keeperSec").html();

			//Wicket count

				remWickCounter     = parseInt($wicCnt)  - 1;

				$("#available_keeper").html((remWickCounter));

				$("#available_keeperSec").html((remWickCounter));

		

		}

		

		//Sending data to function for storing details of player removed from list

		var dataString = "id="+id+"&teamId="+teamId+"&op=del";

		$.ajax({		

		type: "Get",

		url: changePlayerUrl,

		data: dataString,

		success: function(html)

	    {			

			//response

			

	    }



	   });

	   

	   //Displaying the selection of player block

	   if(playersCnt < 11)

	   {

			$("#select-card").show();

			$("div#remove-player-btn").hide();	

	   }

};





//function to add a substitute player

function addChangedPlayer(content)

{

	

	details = content.split("-");

	id = details[0];

	

	points = parseInt(details[2]);

	

	//Checking condtion for players already exist

		var playersID = document.getElementsByName('playerID[]');

		for(var i =0;i<playersID.length; i++)

		{

			

			if(playersID[i].value == id )

			{

				alert("Player already present in your team!!!");

				return false;

			}

		}

		var counter = 0;

		$("input[name='teamID']").each(function(){

			if($(this).val() == details[4]){

				counter++;

			}

			});

		if(counter == 3)

		{

				alert("Only 3 players should be selected from one team");

				return false;

		}

	var role = details[3].replace(/^\s+|\s+$/g,"");

	

	$batsCnt = $("#available_batsmanSec").html();

	$bowlCnt = $("#available_bowlerSec").html();

	$wicCnt = $("#available_keeperSec").html();

	//Displaying the count of players remaining

		if(role == 'B' || role == 'A')

		{

	

			//Batsman count

			$batsCnt = $("#available_batsmanSec").html();

			remBatsCounter   = parseInt($batsCnt)+1; 

			//$("#available_batsman").html((remBatsCounter));

			$("#available_batsmanSec").html((remBatsCounter));

		}

		if(role == 'O')

		{

			//Bowler count

				$bowlCnt = $("#available_bowlerSec").html();

				remBowlCounter     =parseInt($bowlCnt)+1 ;

				//$("#available_bowler").html((remBowlCounter));

				$("#available_bowlerSec").html((remBowlCounter));

		}	

		if(role == 'W')

		{

			$wicCnt = $("#available_keeperSec").html();

			//Wicket count

				remWickCounter     = parseInt($wicCnt)  + 1;

				//$("#available_keeper").html((remWickCounter));

				$("#available_keeperSec").html((remWickCounter));

		

		}

		

		$batsCnt = $("#available_batsmanSec").html();

		$bowlCnt = $("#available_bowlerSec").html();

		$wicCnt = $("#available_keeperSec").html();

		

		if($batsCnt > 5)

		{

			alert("Team should have only 5 Batsman");

			remBatsCounter = parseInt($batsCnt) -1; 

			$("#available_batsmanSec").html((remBatsCounter));

			return false;

		}

		if($bowlCnt > 5)

		{

			alert("Team should have only 5 Bowlers");

			remBowlCounter  = parseInt($bowlCnt) - 1;

			$("#available_bowlerSec").html((remBowlCounter));

			return false;

		}

		if($wicCnt > 1)

		{

			alert("Team should have only 1 Wicket Keeper");

			remWickCounter  = parseInt($wicCnt)  - 1;

			$("#available_keeperSec").html((remWickCounter));

		

			return false;

		}

	

	

	

	var imgPath = document.getElementById("imgPath"+id).value;

	if(imgPath!="")

	{
                 var url= getBaseURL();

		var img = '<img src="/files/players/'+imgPath+'" width="40px" /><br>';

	}

	else

	{

		var img ="";

	}

	var type;

	if(role == 'B'|| role == 'A')

	{

		type = 'Batsman';

			

	}

	if(role == 'O')

	{

		type = 'Bowler';

	}

	if(role == 'W')

	{

		type = 'Wicket Keeper';

	}

	if(role == 'A')

	{

		type = 'All Rounder';

	}

	htmlVal = '<div class="my-team-bg"><p>'+img+'</p><p>' + details[1] +'</p><span class="error"> * </span><p>'+ type + '</p>';

	htmlVal += "<span class='error'> * </span><p>" + details[2] + " points </p>";

	htmlVal += '<p><div id="captain-selection" style="display:none;"><input type="radio" name="is_captain[]" id="is_captain" value="'+ id +'" onclick="teamCaptain();"></div></p>';

	htmlVal += '<p><div id="remove-player-btn"><a href="javascript: void(0);" onclick="changePlayer('+ id +','+ points +','+'\''+role+'\''+','+PlayerId+','+details[5]+');"><img src="/cricworld/public/images/btn/rem_player.png" /></a></div></p></div>';

	htmlVal +='<input type="hidden" name="playerID[]" id="playerID" value="'+id+'">';

	htmlVal +='<input type="hidden" name="teamID" id="teamID" value="'+details[4]+'">';

	$("#player-"+ (PlayerId)).html(htmlVal);

	

	playersCnt = playersCnt + 1;

	

	



	//Adding points of the substitute player to team points

		editPointUpdate(points, "add");

		

//	if(playersCnt == 11)

//	{

		if(document.getElementById('used-points').innerHTML >1000)

		{

			alert("Points of player should not exceed more than 1000");	

			

			editPointUpdate(points, "sub");

			

			if(role == 'B'|| role == 'A')

			{				

				remBatsCounter = parseInt($batsCnt) -1; 

				$("#available_batsmanSec").html((remBatsCounter));

				

			}

			if(role == 'O')	

			{

				

				remBowlCounter  = parseInt($bowlCnt) - 1;

				$("#available_bowlerSec").html((remBowlCounter));

				

			}

			if(role == 'W')

			{

				remWickCounter  = parseInt($wicCnt)  - 1;

				$("#available_keeperSec").html((remWickCounter));

			

				

			}

			

			$("#player-"+ (PlayerId)).html('');			

			playersCnt = playersCnt - 1;

			return false;

		}

		else

		{			

			$("#select-card").hide();

			$("#edit-team-step").show();

			$("div#remove-player-btn").show();

			

		}

		

	//}

	

//Sending data to function for saving data of substitute player

	

	var dataString = "id="+id+"&teamId="+details[5]+"&op=add";

	$.ajax({		

	type: "Get",

	url: changePlayerUrl,

	data: dataString,

	success: function(html)

    {		

	 	//response

	 	if(html == 5)

	 	{

	 		$("div#sub-team-step").show();

	 		$("div#remove-player-btn").hide();

	 		

	 	}

    }



   });

}



//function to select captain 

var teamCaptain = function()

{

	$("#create-team-step").show();

	$("#select-captain-step").hide();

	

};





var pointUpdate = function(point, operation){

	

	

		if(operation == "add"){

			currentTeamValue += point;

			document.getElementById('used-points').innerHTML = "";

			$("#used-points").html(currentTeamValue);

			document.getElementById('balance-points').innerHTML = "";

			$("#balance-points").html((totalValue-currentTeamValue));

		}else if(operation == "sub"){

			

			currentTeamValue -= point;			

			document.getElementById('used-points').innerHTML = "";

			$("#used-points").html(currentTeamValue);

			document.getElementById('balance-points').innerHTML = "";

			$("#balance-points").html((totalValue-currentTeamValue));

			

		}

	

	

	

};



//function to calculate points for substitute player 

var editPointUpdate = function(point, operation){

	

	

	if(operation == "add"){

		currentTeamValue -= parseInt(point);	

		var usedPoints  = document.getElementById('used-points').innerHTML;

		$("#used-points").html(parseInt(usedPoints) + parseInt(point));

		var balancePoints = document.getElementById('balance-points').innerHTML;

		$("#balance-points").html(currentTeamValue);

	}else if(operation == "sub"){

				

		currentTeamValue += parseInt(point);

		var usedPoints  = document.getElementById('used-points').innerHTML;

		$("#used-points").html(parseInt(usedPoints) - parseInt(point));

		document.getElementById('balance-points').innerHTML = "";

		$("#balance-points").html((currentTeamValue));

		

		

		

	}







};

//function to clear team divs

var clearTeamDivs = function()

{

	document.getElementById('divName').innerHTML = "";

	document.getElementById('divCode').innerHTML = "";

};





//function to validate team form

var validateTeam = function(){

	

	

	//function to clear team divs

	clearTeamDivs();

	

	var name = document.getElementById('name').value;

	var code = document.getElementById('code').value;

	

	var counter = 0;

	if(name == "")

	{

		document.getElementById('divName').innerHTML =  "<span class='error'>Please Enter Team Name</span>";

		counter = counter + 1;

	}

	if(code == "")

	{

		document.getElementById('divCode').innerHTML =  "<span class='error'>Please Enter Team Code</span>";

		counter = counter + 1;

	}

	

	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

};

//function to validate player form

var validatePlayer = function()

{

	

	clearPlayerDivs();

	var counter = 0;

	//get all div objects

	//var objDivName 			    = document.getElementById('nameDiv');

	var objDivFirstName 		= document.getElementById('firstNameDiv');

	var objDivLastName 			= document.getElementById('lastNameDiv');

	var objDivBatting 			= document.getElementById('battingStyleDiv');

    var objDivBowling			= document.getElementById('bowlingStyleDiv');

    var objDivPosition			= document.getElementById('allRounderDiv');

    //var objDivBaseValue			= document.getElementById('baseValueDiv');

    var objDivPoint				= document.getElementById('pointDiv');

    var objDivTeam				= document.getElementById('teamDiv');

    

    //var objName  				= document.getElementById('name').value;

    var objFirstName  			= document.getElementById('firstName').value;

    var objLastName  			= document.getElementById('lastName').value;

    var objBatting  			= document.getElementById('battingStyle').value;

    var objBowling				= document.getElementById('bowlingStyle').value;

    //var objBaseValue			= document.getElementById('baseValue').value;

    var objPoint				= document.getElementById('point').value;

    var objPosition				= document.getElementById('drpStatus').value;

    var objTeam					= document.getElementById('teamNames').value;

    

   

    

    if(objFirstName == "")

    {

    	objDivFirstName.innerHTML = "<span class='error'>Please Enter First Name</span>";

        counter=counter+1;

    }

    if (objFirstName!="")

    {

    	if(!IsAlphabets(objFirstName))

    	{

    		objDivFirstName.innerHTML = "<span class='error'>Please Enter Alphabets Only</span>";

	        counter=counter+1;

    	}

       

    }

    if(objLastName == "")

    {

    	objDivLastName.innerHTML = "<span class='error'>Please Enter Last Name</span>";

        counter=counter+1;

    }

    if (objLastName!="")

    {

    	if(!IsAlphabets(objLastName))

    	{

    		objDivLastName.innerHTML = "<span class='error'>Please Enter Alphabets Only</span>";

	        counter=counter+1;

    	}

       

    }

    if(objBatting == "")

    {

    	objDivBatting.innerHTML = "<span class='error'>Please Enter Batting Style</span>";

        counter=counter+1;

    }

    

    if(objBowling == "")

    {

    	

    	objDivBowling.innerHTML = "<span class='error'>Please Enter Bowling Style</span>";

        counter=counter+1;

    }

    /*if(objBaseValue == "")

    {

    	objDivBaseValue.innerHTML = "<span class='error'>Please Enter Base Value</span>";

        counter=counter+1;

    }

    if (objBaseValue!="")

    {

    	

    	if(!IsNumerics(objBaseValue))

    	{

    		objDivBaseValue.innerHTML = "<span class='error'>Please Enter Numbers Only</span>";

	        counter=counter+1;

    	}

    	 

    }*/

    

    if(objPoint == "")

    {

    	objDivPoint.innerHTML = "<span class='error'>Please Enter Point</span>";

        counter=counter+1;

    }

    if (objPoint!="")

    {

    	

    	if(!IsNumerics(objPoint))

    	{

    		objDivPoint.innerHTML = "<span class='error'>Please Enter Numbers Only</span>";

	        counter=counter+1;

    	}

    	 

    }

  

    if(objPosition == "0")

    {

    	objDivPosition.innerHTML = "<span class='error'>Please Enter Role of Player</span>";

        counter=counter+1;

    }

    

    if(objTeam == "0")

    {

    	

    	objDivTeam.innerHTML = "<span class='error'>Please select team for player.</span>";

        counter=counter+1;

    }

  //Checking condition whether counter is  zero   

    if(counter >0)

    {

    	

    	document.getElementById('errorMsg').innerHTML = "<span class='error'>Please fill in the Required Fields.</span>";

    	return false;

    }

    else

    {

    	return true;

    }

   

    

}

//function to clear player form divs

var clearPlayerDivs = function()

{

	

	document.getElementById('firstNameDiv').innerHTML 		= "";

	document.getElementById('lastNameDiv').innerHTML 		= "";

	document.getElementById('battingStyleDiv').innerHTML 	= "";

    document.getElementById('bowlingStyleDiv').innerHTML 	= "";

    document.getElementById('allRounderDiv').innerHTML 		= "";

    //document.getElementById('baseValueDiv').innerHTML 		= "";

    document.getElementById('pointDiv').innerHTML 			= "";

    document.getElementById('teamDiv').innerHTML 			= "";

}

var ValidatePlayer = function()

{

	var team 	= document.getElementById('teamNames').value;

	var player  = document.getElementById('playerNames').value;

	

	var counter = 0;

	if(team == "0")

	{

		document.getElementById('teamDiv').innerHTML =  "<span class='error'>Please Select Team Name</span>";

		counter = counter + 1;

	}

	if(player == "")

	{

		document.getElementById('playerDiv').innerHTML =  "<span class='error'>Please Select Player Name</span>";

		counter = counter + 1;

	}

	

	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

	

}

//function is used to check the string is Alphabers

function IsAlphabets(str) {

    var tempString="";

    var regExp = /^[A-Z a-z]$/;

    if(str != null && str != "")

    {

      for(var i = 0; i < str.length; i++)

      { 

        if (!str.charAt(i).match(regExp))

        {

          return false;

        }

      }

    }

    else

    {

      return false;

    }

    return true;

}

//function is used to check the string is Numeric

function IsNumerics(sText) {

    var ValidChars = "0123456789.";

    var IsNumber = true;

    var Char;





    for (i = 0; i < sText.length && IsNumber == true; i++) {

        Char = sText.charAt(i);

        if (ValidChars.indexOf(Char) == -1) {

            IsNumber = false;

        }

    }

    return IsNumber;



}



var validatePlayerImages = function()

{

	//clearing divs

	document.getElementById('div_name').innerHTML        =  "";

	document.getElementById('div_document').innerHTML    =  "";

	

	//retrieving fields

	var name         = document.getElementById('name').value;

	var file        = document.getElementById('document').value;

	var counter      = 0;

	

	//check for name

	if(name == "")

	{



		document.getElementById('div_name').innerHTML = "<span class='error'>Please Enter Name</span>";

		counter=counter+1;

	

	}

	//check for document

	if(file == "")

	{

		document.getElementById('div_document').innerHTML = "<span class='error'>Please Upload Image</span>";

		counter=counter+1;

	}



	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

};

//code to validate league 

var validateLeague = function()

{

	//clearing divs

	document.getElementById('divLeagueName').innerHTML    =  "";

	document.getElementById('tournamentDiv').innerHTML    =  "";

	document.getElementById('statusDiv').innerHTML    =  "";

	

	

	//retrieving fields

	var leagueName          = document.getElementById('league_name').value;

	var tournament          = document.getElementById('tournament').value;

	var publicstatus    	= $('input[type=radio][id=public_status]:checked').val();

	var privatestatus    	= $('input[type=radio][id=private_status]:checked').val();

	var counter      = 0;

	

	//check for name

	if(leagueName == "")

	{



		document.getElementById('divLeagueName').innerHTML = "<span class='error'>Please Enter League Name.</span>";

		counter=counter+1;

	

	}

	//check for tournament

	if(tournament == "0")

    {

		document.getElementById('tournamentDiv').innerHTML = "<span class='error'>Please Select Tournament.</span>";

        counter=counter+1;

    }

	//check for status

	if(!publicstatus && !privatestatus )

	{

		

			document.getElementById('statusDiv').innerHTML = "<span class='error'>Please check whether it is public or Private.</span>";

			counter=counter+1;



	

	}

	

	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

};





//function to check duplicate of team

var checkTeam = function(name)

{

	

	var dataString = "name="+name;

			$.ajax({		

			   type: "Get",

			   url: teamUrl,

			   data: dataString,

			    success: function(html)

			    {

					

					document.getElementById('divName').innerHTML = "<span class='error'>"+html+"</span>";

					

					

			    }

		});

};

//function to check duplicate of user team

var checkUserTeam = function(name)

{

	

	var dataString = "name="+name;

			$.ajax({		

			   type: "Get",

			   url: userTeamUrl,

			   data: dataString,

			    success: function(html)

			    {

					

					document.getElementById('divUserTeam').innerHTML = "<span class='error'>"+html+"</span>";

					

					

			    }

		});

};

//function to set top status player

var setTopPlayer = function(status,playerId)

{

	if(status.checked  == true)

	{

		var status = 'Y';

	}

	else

	{

		var status = 'N';

	}

	var dataString = "status="+status+"&playerId="+playerId;

	$.ajax({		

	   type: "Get",

	   url: topStatusUrl,

	   data: dataString,

	    success: function(html)

	    {

			

	    }

});

};





//function to do filteration based on team names in players list



var showTeamResults = function(teamId,page)

{

	var dataString = "id="+teamId+"&page="+page;

	$.ajax({		

	   type: "Get",

	   url: showTeamUrl,

	   data: dataString,

	    success: function(html)

	    {

		 document.getElementById('playerDiv').innerHTML = html;

	    }

});

};

//function to validate Banner Images

var validateBanners = function()

{

	

	//clearing divs

	document.getElementById('div_url').innerHTML        =  "";

	document.getElementById('div_document').innerHTML    =  "";

	

	//retrieving fields

	var name         = document.getElementById('url').value;

	var file        = document.getElementById('document').value;

	var counter      = 0;

	

	//check for name

	if(name == "")

	{



		document.getElementById('div_url').innerHTML = "<span class='error'>Please Enter Landing Url</span>";

		counter=counter+1;

	

	}

	//check for document

	if(file == "")

	{

		document.getElementById('div_document').innerHTML = "<span class='error'>Please Upload Banner</span>";

		counter=counter+1;

	}



	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

};

//function to display match schedule

var displayMatchSchedule = function(teamId)

{



	var dataString = "id="+teamId;

	$.ajax({		

	   type: "Get",

	   url: showMatchUrl,

	   data: dataString,

	    success: function(html)

	    {

		

		 document.getElementById('matchDiv').innerHTML = html;

	    }

});

};

//function change captain

function changeCaptain(id,teamId)

{

	var dataString = "id="+id+"&teamId="+teamId;

	$.ajax({		

	   type: "Get",

	   url: changeCaptainUrl,

	   data: dataString,

	   success: function(html)

	   {			

			$("div#captain-selection").hide();

	   }

	});

}

function enableRadioBtn()

{

	$("div#captain-selection").show();

}

//function to validate change password

var validatePassword = function()

{

	var oldPwd		= document.getElementById('old_pwd').value;

	var newPwd 	    = document.getElementById('new_pwd').value;

	var confirmPwd  = document.getElementById('confirm_pwd').value;

	

	var counter = 0;

	if(newPwd != confirmPwd)

	{

		document.getElementById('confirmDiv').innerHTML =  "<span class='error'>New password and confirm password are not same</span>";

		counter = counter + 1;

	}

	if(oldPwd == "")

	{

		document.getElementById('oldDiv').innerHTML =  "<span class='error'>Please enter old password.</span>";

		counter = counter + 1;

	}

	

	if(counter > 0)

	{

		return false;

	}

	else

	{

		return true;

	}

	

};


function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}